//定义store
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

// 定义登录信息的类型
interface LoginInfo {
    redirectUrl: string
    token: string
}

export const loginInfoStore = defineStore('loginInfoStore', () => {

    //定义响应式变量 - 存储完整的登录信息对象
    const loginInfo = ref<LoginInfo | null>(null)

    //定义修改登录信息函数
    const setLoginInfo = (newLoginInfo: LoginInfo) => {
        loginInfo.value = newLoginInfo
    }

    //定义删除登录信息函数
    const removeLoginInfo = () => {
        loginInfo.value = null
    }

    // 计算属性：便捷访问 redirectUrl
    const redirectUrl = computed(() => {
        return loginInfo.value?.redirectUrl || ''
    })

    // 计算属性：便捷访问 token
    const token = computed(() => {
        return loginInfo.value?.token || ''
    })

    // 计算属性：检查是否已登录
    const isLoggedIn = computed(() => {
        return !!loginInfo.value?.token
    })

    return {
        loginInfo,
        setLoginInfo,
        removeLoginInfo,
        redirectUrl,
        token,
        isLoggedIn
    }
}
)